package cn.xfdzx.android.apps.shop.presenter;

import cn.xfdzx.android.apps.shop.base.BasePresenter;
import cn.xfdzx.android.apps.shop.bean.CategoryAllBeen;
import cn.xfdzx.android.apps.shop.bean.HomeSearchXFDGoodBeen;
import cn.xfdzx.android.apps.shop.contract.ClassProductContract;
import cn.xfdzx.android.apps.shop.contract.OneCategoryContract;
import cn.xfdzx.android.apps.shop.model.ClassProductModel;
import cn.xfdzx.android.apps.shop.model.OneCategoryModel;
import cn.xfdzx.android.apps.shop.net.RxScheduler;
import io.reactivex.functions.Consumer;

/**
 * @author
 * @date Description：
 */
public class ClassProductPresenter extends BasePresenter<ClassProductContract.View> implements ClassProductContract.Presenter {

    private ClassProductContract.Model model;

    public ClassProductPresenter() {
        model = new ClassProductModel();
    }


    @Override
    public void initData(boolean isRefresh, boolean isLoading,String lng,  String lat, String sid, String page, String limit, String index_status, String is_store, String store_id, String keyword, String priceOrder, String salesOrder, String news) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        if (isLoading){
            mView.showLoading();
        }
        model.initData(lng,lat,sid, page, limit, index_status, is_store, store_id, keyword, priceOrder, salesOrder, news).compose(RxScheduler.<HomeSearchXFDGoodBeen>Flo_io_main())
                .as(mView.<HomeSearchXFDGoodBeen>bindAutoDispose()).subscribe(new Consumer<HomeSearchXFDGoodBeen>() {
            @Override
            public void accept(HomeSearchXFDGoodBeen baseObjectBean) throws Exception {
                mView.onInitSuccess(baseObjectBean);
                if (isLoading){
                    mView.hideLoading();
                }
                if (isRefresh){
                    mView.finishRefresh();
                }else {
                    mView.finishLoadMore();
                }
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                mView.onError(throwable);
                if (isLoading){
                    mView.hideLoading();
                }
                if (isRefresh){
                    mView.finishRefresh();
                }else {
                    mView.finishLoadMore();
                }
            }
        });
    }
}
